package cn.cdut.yiwenqian.user.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.cdut.yiwenqian.biz.interfaces.IPostManager;
import cn.cdut.yiwenqian.entity.CdutPostTitles;
import cn.cdut.yiwenqian.entity.CdutPostTitletypes;
import cn.cdut.yiwenqian.entity.CdutPostUsers;
import cn.cdut.yiwenqian.util.MyPageToolVo;
@Controller
@Namespace(value="/post")
public class PostManagerAction extends ActionSupport {
	
	@Resource
	private IPostManager iPostManager;
	
	private Integer pageNum;
	private String keyword;
	private String parentId;
	private String name;

	public String creatTitlePost(){
		return "success";
	}
	@Action(value="findAllUserPost",results={@Result(name="success",location="/WEB-INF/jsp/user/MyPost.jsp"),@Result(name="error",location="/WEB-INF/jsp/hello.jsp")})
	public String findAllUserPost(){
		pageNum=pageNum==null?1:pageNum;
		CdutPostUsers user=(CdutPostUsers) ActionContext.getContext().getSession().get("user");
		
		if(user!=null){
			MyPageToolVo<CdutPostTitles> page= iPostManager.findAllUesrsPage(pageNum, user.getUnumber());
			try {
				
				ServletActionContext.getRequest().setAttribute("page", page);
			} catch (NullPointerException e) {
				ServletActionContext.getRequest().setAttribute("message", "没有登录");
				return "error";
			}
		}
		return "success";
		
	}
	@Action(value="/findTitleTypeByKeyword" ,results={@Result(name="success",location="/WEB-INF/jsp/post/CreatePostPage.jsp")})
	public String findTitleTypeByKeyword(){
		keyword=keyword==null?"":keyword;
		List<CdutPostTitletypes> list=iPostManager.findTypeByKeyword(keyword);
			ServletActionContext.getRequest().setAttribute("list", list);
			return "success";
	}
	@Action(value="/createNewType",results={@Result(name="success",location="/WEB-INF/jsp/hello.jsp")})
	public String createNewType(){
		parentId=parentId==null?"0":parentId;
		CdutPostTitletypes cptt=new CdutPostTitletypes();
		cptt.setName(name);
		cptt.setParentnumber(parentId);
		iPostManager.saveTitleType(cptt);
		ServletActionContext.getRequest().setAttribute("message", "创建成功！");
		return "success";
	}
	/**
	 * @return the iPostManager
	 */
	public IPostManager getiPostManager() {
		return iPostManager;
	}
	/**
	 * @param iPostManager the iPostManager to set
	 */
	public void setiPostManager(IPostManager iPostManager) {
		this.iPostManager = iPostManager;
	}
	/**
	 * @return the pageNum
	 */
	public Integer getPageNum() {
		return pageNum;
	}
	/**
	 * @param pageNum the pageNum to set
	 */
	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}
	/**
	 * @return the keyword
	 */
	public String getKeyword() {
		return keyword;
	}
	/**
	 * @param keyword the keyword to set
	 */
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	/**
	 * @return the parentId
	 */
	public String getParentId() {
		return parentId;
	}
	/**
	 * @param parentId the parentId to set
	 */
	public void setParentId(String parentId) {
		this.parentId = parentId;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
}
